<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Canvas</title>
    <style type="text/css">
        body, html {
            margin: 0;
        }
        canvas {
            position: absolute;
            left: 0;
        }
    </style>
</head>
<body onload="draw()">
    <img src="img.jpg" alt=""/>
    <canvas id="canvas" width="534" height="300">
        你的浏览器太 low 了，请更新~!
    </canvas>
<script>
    function draw() {
        var canvas = document.getElementById("canvas");
        if (canvas.getContext) {
            var ctx = canvas.getContext('2d');

            // 1. 将图片用灰色盖住
            ctx.beginPath();
            ctx.fillStyle = 'gray';
            ctx.fillRect(0, 0, canvas.width, canvas.height);

            ctx.globalCompositeOperation = 'destination-out';
            ctx.lineWidth = 20;
            ctx.lineCap = 'round';

            canvas.onmousedown = function (e) {
                var ev = e || window.event;
                var x = ev.clientX;
                var y = ev.clientY;

                ctx.moveTo(x, y);

                canvas.onmousemove = function (e) {
                    var ev = e || window.event;
                    var x = ev.clientX;
                    var y = ev.clientY;

                    ctx.lineTo(x, y);
                    ctx.stroke();
                };

                canvas.onmouseup = function () {
                    canvas.onmousemove = '';
                }
            }
        }
    }
</script>
</body>
</html>
